﻿using Anderson.NetLobby.Common;
using Anderson.NetLobby.MessageContent;
using System;
using System.Text;
using UnityEngine;

public class LobbyTest01 : MonoBehaviour {

	// Use this for initialization
	void Start () {
        //TestBufferCopy();
        //TestClientL1Mask();

        //TestLogNetMessage();
    }

    private void Update()
    {
        if (Time.frameCount % 100 == 0)
        {
            Color c = UnityEngine.Random.ColorHSV();
            GetComponent<Renderer>().material.color = c;
        }
    }

    void TestLogNetMessage()
    {
        MC_PlayerId setPlayerId = new MC_PlayerId();
        setPlayerId.idFromServer = 1;

        NetMessage<MC_PlayerId> netMessage = new NetMessage<MC_PlayerId>(
            0L,
            MessageLevelDefinition.SERVER_L1_SYSTEM,
            MessageLevelDefinition.SERVER_L2_SYSTEM_PLAYERID,
            MessageLevelDefinition.SERVER_L3_SYSTEM_PLAYERID_CMD,
            setPlayerId);

        byte[] bytes = MessageSerialize.Serialize(netMessage);

        StringBuilder s = new StringBuilder();
        for (int i = 0; i < bytes.Length; i++)
        {
            s.Append(bytes[i]);
        }

        Debug.Log(s.ToString());

        //=============

        NetMessage<MC_PlayerId> netMessage1 = MessageSerialize.Deserialize<MC_PlayerId>(bytes);
        MC_PlayerId setPlayerId1 = netMessage1.MessageContent;

        Debug.Log(netMessage1.MessageBOF);
        Debug.Log(setPlayerId1.idFromServer);
        Debug.Log(netMessage1.MessageEOF);

    }

    private void TestClientL1Mask()
    {
        uint x1 = 0x02800001;
        uint x2 = 0x02800102;
        uint x3 = 0x0280ABCD;
        uint y1 = 0x01800000;
        uint y2 = 0x02700000;
        uint y3 = 0x028A0000;

        uint[] array = new uint[] { x1, x2, x3, y1, y2, y3};

        foreach (uint item in array)
        {
            uint v = item & MessageLevelDefinition.MASK_CLIENT_L1_BIZ;
            if (v == MessageLevelDefinition.CLIENT_L1_BIZ)
            {
                Debug.LogFormat("0x{0:X8} = {1}", item, true);
            }
            else
            {
                Debug.LogFormat("0x{0:X8} = {1}", item, false);
            }
        }

    }

    void TestBufferCopy()
    {
        int count = 8;

        byte[] buffer = new byte[count];
        for (byte i = 0; i < count; i++)
        {
            buffer[i] = i;
        }

        byte[] temp = new byte[count / 2];
        Buffer.BlockCopy(buffer, 0, temp, 0, count / 2);

        string msg = "";
        foreach (byte item in temp)
        {
            msg += item + ",";
        }

        msg += "\r\n";
        Buffer.BlockCopy(buffer, count / 2, temp, 0, count / 2);
        foreach (byte item in temp)
        {
            msg += item + ",";
        }

        Debug.Log(msg);
    }
}
